#!/usr/bin/env python3
"""
为现有用户设置密码
"""

import os
import sys
import django

# 添加backend目录到Python路径
backend_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'backend')
sys.path.insert(0, backend_path)

# 设置Django环境
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'HopeEngine.settings')
django.setup()

from accounts.models import User

def set_user_password():
    """为现有用户设置密码"""
    print("=== 为现有用户设置密码 ===")
    
    try:
        # 获取第一个用户
        user = User.objects.first()
        if user:
            print(f"为用户 {user.username} 设置密码")
            user.set_password('test')  # 设置密码为 'test'
            user.save()
            print(f"✅ 密码设置成功")
            
            # 测试认证
            from django.contrib.auth import authenticate
            auth_user = authenticate(username=user.username, password='test')
            if auth_user:
                print(f"✅ 认证测试成功")
            else:
                print(f"❌ 认证测试失败")
        else:
            print("❌ 没有找到用户")
            
    except Exception as e:
        print(f"❌ 设置密码失败: {e}")

def main():
    """主函数"""
    print("开始设置密码...\n")
    set_user_password()
    print("\n=== 完成 ===")

if __name__ == "__main__":
    main() 